//
//  MeViewController.m
//  Job
//
//  Created by 桑赐相 on 2018/8/13.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "MeViewController.h"
#import "TableViewModel.h"
#import "BasesTableViewCell.h"
#import "MeHeaderTableViewCell.h"

@interface MeViewController ()
@property (nonatomic,strong) XLUserModel *user;
@end

@implementation MeViewController
-(instancetype)initWithStyle:(UITableViewStyle)style{
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 0);
    self.tableView.mj_header = nil;
    self.tableView.mj_footer = nil;
    [self.dataSource addObjectsFromArray:[TableViewModel myTableViewData]];
    [self.tableView registerNib:[UINib nibWithNibName:@"BasesTableViewCell" bundle:nil] forCellReuseIdentifier:@"BasesTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MeHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"MeHeaderTableViewCell"];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.user = [XLUserModel getUserInfo];
    [self reloadData];
}
-(void)loginAction{
    
    UIViewController *login = SBView(@"Login", @"LoginViewController");
    [self pushViewController:login];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count + 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return [self.dataSource[section-1] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        MeHeaderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeHeaderTableViewCell" forIndexPath:indexPath];
        cell.user = self.user;
        return cell;
    }else{
        BasesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BasesTableViewCell" forIndexPath:indexPath];
        TableViewModel *model = self.dataSource[indexPath.section-1][indexPath.row];
        cell.model = model;
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section == 0) {
        if (!self.user) {
            [self loginAction];
            return;
        }
        [self pushController:@"MyInfoController"];
    }else{
        if (indexPath.section == 1 || (indexPath.section == 2 && indexPath.row == 0)) {
            if (!self.user) {
                [self loginAction];
                return;
            }
        }
        NSArray *array = self.dataSource[indexPath.section - 1];
        TableViewModel *model = array[indexPath.row];
        [self pushController:model.className];
    }
}

-(void)pushController:(NSString *)className{
    Class class = NSClassFromString(className);
    if (class) {
        UIViewController *view = [[class alloc]init];
        [self pushViewController:view];
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 120.0 : 44.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section == 0 ? 0.0001 : 10.0;
}


















@end
